# coding:utf-8
'''
author:wangyi
'''

import pandas as pd
import numpy as np
import pickle
import random

def trans_data_to_one(inf,outf):
    df = pd.read_csv(inf)
    datas = [list(df.iloc[:,0]),list(df.iloc[:,1])]
    for i in range(2,df.shape[1]):
        m = list(df.iloc[:,i])
        max_v = np.max(m)
        m = [(x/max_v) for x in m]
        datas.append(m)
    result = []
    for i in range(len(datas[0])):
        m = []
        for j in range(len(datas)):
            m.append(datas[j][i])
        result.append(m)

    df = pd.DataFrame(result,columns=['日期','时刻','辐照度','风速','风向','温度','湿度','气压'])
    df.to_csv(outf,encoding='utf-8',index=False)


def create_seq_data(inf,outf):
    df = pd.read_csv(inf)
    datas = []
    for i in range(0,df.shape[0],8):
        m = []
        for j in range(8):
            m.append(list(df.iloc[i+j,2:]))
        datas.append(m)
        print(i)
    pickle.dump(datas,open(outf,'wb'))


def create_train_val_data(inf,y_inf,outf):
    datas = pickle.load(open(inf,'rb'))
    df = pd.read_csv(y_inf)
    y = np.asarray(list(df.iloc[:,1]))
    indexs = [i for i in range(len(datas))]
    random.shuffle(indexs)
    datas = np.asarray(datas)[indexs]
    y = y[indexs]
    train_datas = []
    val_datas = []
    y_train = []
    y_val = []
    for i in range(len(datas)):
        if i <= int(len(datas)*0.9):
            train_datas.append(datas[i])
            y_train.append([y[i]])
        else:
            val_datas.append(datas[i])
            y_val.append([y[i]])
        print(i)
    pickle.dump([train_datas,val_datas,y_train,y_val],open(outf,'wb'))

if __name__ == '__main__':
    # trans_data_to_one('../datasets/train_feature.csv','../datasets/train_feature_to_1.csv')
    # trans_data_to_one('../datasets/test_feature.csv', '../datasets/test_feature_to_1.csv')
    create_seq_data('../datasets/test_feature_to_1.csv','../datasets/test_feature_seq_1.pkl')
    #create_train_val_data('../datasets/train_feature_seq_1.pkl','../datasets/train_label.csv','../datasets/seq_train_val_data_1.pkl')